/*
 * This is a collection of javascript functions for the FoxyBack application.
 */

// Update various fields on an order via an ajax call.
function ajaxEditOrderRow(theButton, logArea, debug) {
	// Get the current id from the status select that was changed.
	var trans_id = theButton.name.replace(/-.*$/, "");
	theButton.disabled = true;

	// The log area is where the ajax response will be inserted.
	if (!logArea) logArea = 'message_container';
	var log = $(logArea);
	log.innerHTML = 'Updating transaction '+trans_id+'...';
	if (debug) alert('about to update transaction '+trans_id);

	// Prepare the edit params and url to the ajax handler.
	var params = '&action=order_row_edit&transaction_id='+trans_id;
	
	// Get the other fields to edit for this transaction.
	// Each of these should only exist once in the document.
	var order_status = document.getElementsByName(trans_id+'-order_status')[0].value;
	params += '&order_status_id=' + encodeURIComponent(order_status);

	var status_notes = document.getElementsByName(trans_id+'-status_notes')[0].value;
	params += '&status_notes=' + encodeURIComponent(status_notes);

	var ship_method = document.getElementsByName(trans_id+'-ship_method')[0].value;
	params += '&ship_method=' + encodeURIComponent(ship_method);

	var ship_tracking_number = document.getElementsByName(trans_id+'-ship_tracking_number')[0].value;
	params += '&ship_tracking_number=' + encodeURIComponent(ship_tracking_number);

	var ship_date = document.getElementsByName(trans_id+'-ship_date')[0].value;
	params += '&ship_date=' + encodeURIComponent(ship_date);

	// Needs to be some way to check that the request was successful. Right now you can't even tell if the url is invalid.
	var url = '/order-ajax-handler?'+params;
	
if (debug) alert('post to handler; '+url);

	// Make the request.
	var ajaxRequest = new Ajax(url, {
		method: 'post',
		update: log
	}).request();

	theButton.disabled = false;
	if (debug) alert('Done with ajax on '+trans_id);
	return true;
}

// Update the status on an order via an ajax call.
function ajaxEditOrderStatus(statusSelect, logArea, debug) {
	// Get the current id from the status select that was changed.
	var trans_id = statusSelect.name.replace(/-.*$/, "");
	statusSelect.disabled = true;

	// The log area is where the ajax response will be inserted.
	if (!logArea) logArea = 'message_container';
	var log = $(logArea);
	log.innerHTML = 'Updating status on transaction '+trans_id+'...';
	if (debug) alert('about to update status on transaction '+trans_id);

	// Prepare the edit params and url to the ajax handler.
	var params = '&action=order_status_edit&transaction_id='+trans_id+'&order_status_id='+statusSelect.value;
	var url = '/order-ajax-handler?'+params;

	// Needs to be some way to check that the request was successful. Right now you can't even tell if the url is invalid.

	// Make the request.
	var ajaxRequest = new Ajax(url, {
		method: 'get',
		update: log
	}).request();

	statusSelect.disabled = false;
	if (debug) alert('Done with ajax on '+trans_id);
	return true;
}

// Update the notes on an order via an ajax call.
function ajaxEditOrderNotes(theButton, logArea, resultArea, debug) {

	// Get the values from the form.
	var trans_id = theButton.form.transaction_id.value;
	var notes = theButton.form.notes.value;
	var ship_tracking_number = theButton.form.ship_tracking_number.value;
	var ship_method = theButton.form.ship_method.value;
	var ship_date = theButton.form.ship_date.value;

	// The log area is where the ajax response will be inserted.
	if (!logArea) logArea = 'message_container';
	var log = $(logArea);
	log.innerHTML = 'Updating notes on transaction '+trans_id+'...';
	if (debug) alert('about to update notes on transaction '+trans_id);

	// Escape the notes for use in a url.
	var enc_notes = encodeURIComponent(notes);
	var enc_ship_tracking_number = encodeURIComponent(ship_tracking_number);
	var enc_ship_method = encodeURIComponent(ship_method);
	var enc_ship_date = encodeURIComponent(ship_date);

	// Prepare the edit params and url to the ajax handler.
	var params = '&action=order_notes_edit&transaction_id='+trans_id+'&notes='+enc_notes;
	params += '&ship_tracking_number='+enc_ship_tracking_number+'&ship_method='+enc_ship_method;
	params += '&ship_date='+enc_ship_date;
	var url = '/order-ajax-handler?'+params;

	// Needs to be some way to check that the request was successful. Right now you can't even tell if the url is invalid.

	// Make the request.
	var ajaxRequest = new Ajax(url, {
		method: 'post',
		update: log
	}).request();

	// Update this area with the new notes.
	// TODO: this needs to be able to handle multiple areas.
	if (resultArea) {
		document.getElementById(resultArea).innerHTML = notes;
	}

	if (debug) alert('Done with ajax on '+trans_id);
	return true;
}

function checkAllOrders(checkall) {
	// Loop through all checkboxes in this form.
	var form_id = checkall.form.id;
	var checkboxes = $(form_id).getElements('input[type=checkbox]');
	for (var i = 0; i < checkboxes.length; i++) {
		// Is this a bulk action checkbox?
		if (checkboxes[i].name.match(/-action_checkbox$/)) {
			if (checkall.checked) {
				checkboxes[i].checked = true;
			}
			else {
				checkboxes[i].checked = false;
			}
		}
	}
	return true;
}

function toggleDisplay(id1, id2, class1, class2, just_show, just_hide) {
	if (class1) {

		// Toggle on classes.
		//var elems1 = getElementsByClass(class1);
		var elems1 = $$('.'+class1);

		for (var i = 0; i < elems1.length; i++) {
			if (elems1[i].style.display == 'none') {
				// show
				elems1[i].style.display = '';
			}
			else if (!just_show) {
				// hide
				elems1[i].style.display = 'none';
			}
		}

		if (class2) {
			var elems2 = $$('.'+class2);
			for (var i = 0; i < elems2.length; i++) {
				if (elems2[i].style.display == 'none') {
					elems2[i].style.display = '';
				}
				else {
					elems2[i].style.display = 'none';
				}
			}
		}
	}
	else {
		// Toggle on ids.
		if (!id2) {
			// Single id, so just toggle it.
			var elem1 = document.getElementById(id1);
			if (elem1.style.display == 'none') {
				elem1.style.display = '';
			}
			else {
				elem1.style.display = 'none';
			}
		}
		else {
			// Two ids, so show one and hide the other.
			var elem1 = document.getElementById(id1);
			var elem2 = document.getElementById(id2);

			if (elem1.style.display == 'none') {
				elem1.style.display = '';
				elem2.style.display = 'none';
			}
			else {
				elem1.style.display = 'none';
				elem2.style.display = '';
			}
		}
	}
	return true;
}

// Ensure that a date follows the yyyy-mm-dd format.
function validateDateFormat(dateField) {
	var thisDate = dateField.value;
	if (!thisDate || thisDate.match(/^\d\d\d\d-\d\d-\d\d( \d\d:\d\d(:\d\d)?)?$/)) {
		return true;
	}
	alert('Dates must be in the YYYY-MM-DD format. The optional time must be like HH:MM[:SS]');
	return false;
}

// Function borrowed from dustindiaz.com
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

